import  random
def discounts(name):
    global b
    if name == product[ran_product]:
        moneys[x] = moneys[x] - b * discount
        b = b * discount
    else:
        moneys[x] = moneys[x] - b
        b = b


def refund():
    global b
    ret = input("是否退货,输入t或T退货，输入其他则不退货：")
    if ret == 't' or ret == 'T':
        del shop_car[-1]
        moneys[x] = moneys[x] + b
        print('恭喜，退款成功！您的卡余额还剩', moneys[x])


def shop(moneys):
    # global money
    global b
    name = input("输入想购买商品名称：")
    if name in product:
        a = product.index(name)
        b = price[a]
        if moneys[x] >= b:
            shop_car.append(name)
            discounts(name)
            print('恭喜，购买成功！您的卡余额还剩', moneys[x])
            refund()
        else:
            print('穷鬼，钱不够！请到其他超市购买。')
    else:
        print('没有这个商品别瞎弄！')


def receipt():
    print("-------------------------------------您的购物小票----------------------------------------")
    print("名称\t\t\t\t\t\t\t数量\t\t\t\t\t\t\t单价\t\t\t\t\t\t\t金额")
    i = y = xll = 0
    sums = 0
    k = 0
    for i in shop_car[0: amount-1]:
        k = k + 1
        shop_name = i
        y = 0
        for ll in shop_car[0: amount-1]:
            if shop_name == ll:
                y = y + 1
        xl = product.index(shop_name)
        xll = price[xl]
        sums = sums + xll
        n = 0
        if k != 0:
            for m in shop_car[0: k-1]:
                if i == m:
                    n = 1
                    break
        if n != 1:
            print(i, "\t\t\t\t\t\t\t", y, "\t\t\t\t\t\t ", xll, "\t\t\t\t\t\t", xll*y)
    print(shop_car[amount-1], "\t\t\t\t\t\t\t", y, "\t\t\t\t\t\t ", xll, "\t\t\t\t\t\t", xll * y)
    print("---------------------------------------------------------------------------------------")
    print(f"购买商品总数量为{amount}", f"购买金额为{money - moneys[x]}元", f"商品总价值为{sums}")
    print(f"您还剩余额为{moneys[x]}元")
    print(f"购买时间为：{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))}")


if __name__ == '__main__':
    product = ["主机", "硬盘", "华为", "小米", "魅族", "耳机", "手套", ]
    price = [5000, 1000, 6999, 3999, 2999, 100, 33, ]
    len_product = len(product)
    ran_product = random.randint(0, len_product-1)
    ran = random.randint(1, 10)
    discount = ran/10
    print("您抽到的折扣商品是%s，抽到了%d折" % (product[ran_product], ran))
    shop_car = []
    moneys = []
    money = int(input("输入您拥有的金额:"))
    moneys.append(money)
    x = moneys.index(money)
    b = 0
    while True:
        shop(moneys)
        c = input('输入Q或者q继续购物,输入其他退出继续购物：')
        if c == 'Q' or c == 'q':
            break
    amount = len(shop_car)
    receipt()
